#include<bits/stdc++.h>
using namespace std;

int n;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
bool st[110][110];
int m;
int g[110][110];
int d[110][110];
void bfs(){
    queue<pair<int,int>> q;
    q.push({1, 1});
    d[1][1] = 0;
    while(!q.empty()){
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++){
            int x = t.first + dx[i];
            int y = t.second + dy[i];
            if (x <= 0 || x > n || y <= 0 || y > m || g[x][y] || st[x][y]) continue;
            
            st[x][y] = true;
            d[x][y] = d[t.first][t.second] + 1;
            q.push({x, y});
        }
    }
}

int main(){
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)    
            cin >> g[i][j];
    bfs();
    cout << d[n][m] << endl;
    return 0;
}